package shuashuami.hb.com.avtivity.agent;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.AClickManagerAdapter2;
import shuashuami.hb.com.avtivity.AbActivity;
import shuashuami.hb.com.entity.agent.ClickerBean;
import shuashuami.hb.com.entity.agent.PagerBean;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.util.LoadType;
import shuashuami.hb.com.view.pull.PullToRefreshLayout;

/**
 * created by zhangqi at 2017/7/10
 */
public class ClickerManagerAct2 extends AbActivity {
    private ListView listView;
    private PullToRefreshLayout mptrl;

    private AClickManagerAdapter2 adapter;
    private List<ClickerBean> mResource;

    private List<ClickerBean> mDisplayResource;

    private EditText edit_next;

    private ImageView image_searchbtn;

    private int currentPage=1;

    private int currentLevel=0;//全部刷手  一级刷手   二级刷手

    private int currentType=1;// 1已认真 2 代表待审核

    public int operateType = LoadType.NO_OPERATE;

    private PagerBean pager;

    private boolean isSearch=false;




    @Override
    protected void initUI() {
        setAbContentView(R.layout.act_aclickmaage2);
        listView= (ListView) findViewById(R.id.lv_listview);
        mptrl= (PullToRefreshLayout) findViewById(R.id.refresh_view);
        edit_next= (EditText) findViewById(R.id.edit_next);
        image_searchbtn= (ImageView) findViewById(R.id.image_searchbtn);

    }

    @Override
    protected void initData() {
        mDisplayResource=new ArrayList<>();
        mResource=new ArrayList<>();
        adapter = new AClickManagerAdapter2(this, mDisplayResource);
        listView.setAdapter(adapter);
        getDataFromSever();

    }

    public void disPlayData(String id){
        mDisplayResource.clear();
        for(int i=0;i<mResource.size();i++){
            if(id.equals("") || mResource.get(i).getId().equals(id)){
                mDisplayResource.add(mResource.get(i));
            }
        }

        adapter.notifyDataSetChanged();
    }

    @Override
    protected void initListener() {
        mptrl.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {


            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                //
                operateType= LoadType.DO_REFERSH;
                currentPage=1;
                getDataFromSever();

            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if(pager!=null && pager.getMaxpage()==pager.getCurrentpage()){
                    mptrl.loadmoreFinish(PullToRefreshLayout.NODATA);
                    return;
                }

                operateType= LoadType.LOAD_MORE;
                currentPage++;
                getDataFromSever();
            }
        });

        edit_next.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    edit_next.bringToFront();
                    isSearch=false;
                }else{
                    image_searchbtn.bringToFront();
                    isSearch=true;
                }

            }
        });

        image_searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isSearch){
                    edit_next.clearFocus();

                    disPlayData(edit_next.getText().toString());
                }
            }
        });
    }

    public void getDataFromSever(){
        HttpOnNextListener<String> httpOnNextListener=new HttpOnNextListener<String>() {
            @Override
            public void onNext(final String s) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        resolveByJson(s);
                    }
                });
            }
        };


        HttpAgentMethods.getMyClicker(httpOnNextListener,currentType,currentLevel,currentPage);
    }


    public void resolveByJson(String str){
        try{
            JSONObject object=new JSONObject(str);
            int status=object.getInt("status");
            if (status==203) {
                LoginHttp loginHttp = new LoginHttp(ClickerManagerAct2.this);
                loginHttp.login();
                return;
            }
            if(status==200) {
                JSONObject content=object.getJSONObject("content");

                PagerBean page = new PagerBean();
                page.setCount(content.getInt("count"));
                page.setMaxpage(content.getInt("maxpage"));
                page.setCurrentpage(content.getInt("currentpage"));
                JSONArray list=content.getJSONArray("list");
                List<ClickerBean> childlist=new ArrayList<>();
                for(int i=0;i<list.length();i++){
                    JSONObject item=list.getJSONObject(i);
                    ClickerBean bean=new ClickerBean();
                    bean.resolveByJson(item);
                    childlist.add(bean);
                }
                page.setList(childlist);

                // 开始解析

                pager = page;
//                // 如果是刷新
                if (operateType == LoadType.DO_REFERSH) {
                    mResource.clear();
                }

                mResource.addAll(page.getList());
            }
            else{
                String message=object.getString("message");
                Toast.makeText(ClickerManagerAct2.this,message,Toast.LENGTH_LONG).show();
            }

            disPlayData("");

            adapter.notifyDataSetChanged();
            // 如果是刷新
            if (operateType == LoadType.DO_REFERSH) {
                mptrl.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
            if (operateType == LoadType.LOAD_MORE) {
                mptrl.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }

            operateType = LoadType.NO_OPERATE;


        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(ClickerManagerAct2.this,"json解析失败",Toast.LENGTH_LONG).show();
        }

    }

//    public void newListView(){
//
//        adapter = new AClickManagerAdapter2(this, mResource);
//        listView.setAdapter(adapter);
//
//    }
}
